package com.googlecode.imanager.client.style.listener;

import com.googlecode.imanager.client.Control;
import com.googlecode.imanager.client.style.Style;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public abstract class StyleWrapper implements StyleListener {

    private static final long serialVersionUID = 1L;
    
    private Style style;
    
    public Style getStyle() {
        return style;
    }

    public void setStyle(Style style) {
        if (this.style != style){
            
            if (style != null){
                if (style.getListener() != null
                        && !style.getListener().equals(this)){
                    throw new IllegalArgumentException("The style is already in use by another component: " + style.getListener());
                }
            }
            
            Style old = this.style;
            
            if (old != null){
                this.style = null;
                old.setListener(null);
            }
            
            if (style != null){
                this.style = style;
                style.setListener(this);
            }
            
            onStyleAttributeChanged(style, StyleAttribute.ALL, style);
        }
    }

    @Override
    public void onStyleAttach(Style style) {
        setStyle(style);
    }

    @Override
    public void onStyleDetach(Style style) {
        if (this.style == style){
            setStyle(null);
        }
    }
    
    public abstract Control getControl();
    
    public abstract String getControlClassName();
    
    public abstract int getStateFlags();

}
